package com.d404.server;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;

import com.alibaba.fastjson.JSON;

import im.beans.Group;
import im.beans.Group_User;
import im.beans.User;
import manager.PersistentManager;

/**
 * Servlet implementation class CreateGroup
 */
//@WebServlet("/CreateGroup")
public class CreateGroup extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public CreateGroup() {
        super();
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		request.setCharacterEncoding("UTF-8");
		String data = request.getParameter("data");
		Upload upload = JSON.parseObject(data,Upload.class);
		Download download = new Download();
		
		String status = "9";
		
		
		SessionFactory sf = PersistentManager.sharemanager().getSessionFactory();
//		Session session = sf.openSession();
		Session session = sf.getCurrentSession();
		Transaction tx = session.beginTransaction();
		try
		{
			// 创建群组
			Group group = new Group();
			group.setCreateTime(upload.getCreateTime());
			group.setGroupName(upload.getGroupName());
			group.setOwnerID(Long.parseLong(upload.getUserID()));
			group.setOwnerName(upload.getUserName());
			
			// 创建群组用户表项
			Group_User gu = new Group_User();
			gu.setGroup(group);
			User user = new User();
			user.setId(Long.parseLong(upload.getUserID()));
			gu.setUser(user);
			gu.setCreateGroupUserTimestamp(System.currentTimeMillis());
			
			// 设置群组所属用户
			User user2 = new User();
			user2.setId(Long.parseLong(upload.getUserID()));
			gu.setOwner(user2);
			
			session.save(group);
			session.save(gu);
			
			download.setGroupID(group.getId());
			download.setStatus(Download.SUCCESS);
			session.getTransaction().commit();
			
		}catch(Exception e)
		{
			tx.rollback();
			download.setStatus(Download.FAILD);
			e.printStackTrace();
		}finally {
//			session.close();
		}
		
		System.out.println(JSON.toJSONString(download));
		response.setCharacterEncoding("UTF-8");
		response.getWriter().append(JSON.toJSONString(download));
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doGet(request, response);
	}

	
	 // 上行数据
    public static class Upload
    {
        String userID;
        String userName;
        String groupName;
        long createTime;

        public Upload() {
        }

        public Upload(String userID, String userName, String groupName) {
            this.userID = userID;
            this.userName = userName;
            this.groupName = groupName;
            if (createTime == 0L) createTime = System.currentTimeMillis();
        }

        public String getUserID() {
            return userID;
        }

        public void setUserID(String userID) {
            this.userID = userID;
        }

        public String getUserName() {
            return userName;
        }

        public void setUserName(String userName) {
            this.userName = userName;
        }

        public String getGroupName() {
            return groupName;
        }

        public void setGroupName(String groupName) {
            this.groupName = groupName;
        }

        public long getCreateTime() {
            return createTime;
        }

        public void setCreateTime(long createTime) {
            this.createTime = createTime;
        }
    }

    // 下行数据
    public static class Download
    {
        public static final long SUCCESS = 1;
        public static final long FAILD = 2;

        long status;
        long groupID;

        public long getStatus() {
            return status;
        }

        public void setStatus(long status) {
            this.status = status;
        }

        public long getGroupID() {
            return groupID;
        }

        public void setGroupID(long groupID) {
            this.groupID = groupID;
        }
    }
}
